package com.test.auth.service;

import com.test.auth.domain.MemberDetails;
import com.test.auth.feign.UmsMemberFeignService;

import com.test.common.api.CommonResult;
import com.test.model.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class DemoUserDetailService implements UserDetailsService {

    @Autowired
    private UmsMemberFeignService umsMemberFeignService;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        

        // 加载用户信息
        if (StringUtils.isEmpty(username)) {
            log.warn("用户登陆用户名为空:{}", username);
            throw new UsernameNotFoundException("用户名不能为空");
        }
        
        User umsMember = getByUsername(username);
        
        if (null == umsMember) {
            log.warn("根据用户名没有查询到对应的用户信息:{}", username);
        }
        
        log.info("根据用户名:{}获取用户登陆信息:{}", username, umsMember);
        
        // 会员信息的封装 implements UserDetails
        MemberDetails memberDetails = new MemberDetails(umsMember);
        
        return memberDetails;
    }



    public User getByUsername(String username) {


        CommonResult<User> memberResult = umsMemberFeignService.loadUserByUsername(username);


        return memberResult.getData();

    }
}
